package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysDictTypeVo;
import com.ruoyi.system.domain.bo.SysDictTypeBo;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典类型
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dictType")
public class SysDictTypeController extends BaseController {

    private final ISysDictTypeService iSysDictTypeService;

    /**
     * 查询字典类型列表
     */
    @SaCheckPermission("system:dictType:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictTypeVo> list(SysDictTypeBo bo, PageQuery pageQuery) {
        return iSysDictTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出字典类型列表
     */
    @SaCheckPermission("system:dictType:export")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysDictTypeBo bo, HttpServletResponse response) {
        List<SysDictTypeVo> list = iSysDictTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "字典类型", SysDictTypeVo.class, response);
    }

    /**
     * 获取字典类型详细信息
     *
     * @param dictId 主键
     */
    @SaCheckPermission("system:dictType:query")
    @GetMapping("/{dictId}")
    public R<SysDictTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dictId) {
        return R.ok(iSysDictTypeService.queryById(dictId));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dictType:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysDictTypeBo bo) {
        return toAjax(iSysDictTypeService.insertByBo(bo));
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dictType:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDictTypeBo bo) {
        return toAjax(iSysDictTypeService.updateByBo(bo));
    }

    /**
     * 删除字典类型
     *
     * @param dictIds 主键串
     */
    @SaCheckPermission("system:dictType:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dictIds) {
        return toAjax(iSysDictTypeService.deleteWithValidByIds(Arrays.asList(dictIds), true));
    }
}
